package com.gopay.bis.service.bank.impl.ebank;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.ebank.BocB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BocConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.boc.ebank.BocB2cMessage;
import com.gopay.common.domain.bank.boc.ebank.BocDayEndMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;

/**
 * @BocB2cBankServiceImpl.java
 * @author wanggang
 * @2013-11-4 下午2:19:38 www.gopay.com.cn Inc.All rights reserved.
 */
public class BocB2cBankServiceImpl implements BankInteractRemoteService {

	@Resource(name = "bocB2cManager")
	private BocB2cManager bocB2cManager;
	@Override
	public Result pay(BaseMessage base) {
		Result result = new Result(false);
		try {
			BocB2cMessage messageB2C = new BocB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			messageB2C = (BocB2cMessage) bocB2cManager.sign(messageB2C);
			base.setBankOrderId(base.getOrderId());
			result.setDefaultModel(messageB2C);
			result.setSuccess(true);
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
		}
		return result;
	}

	@Override
	public Result bankBack(BaseMessage base) {
		Result result = new Result(false);
		try {
			BocB2cMessage messageB2C = new BocB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			boolean flag = (Boolean) bocB2cManager.verify(messageB2C);
			result.setDefaultModel(flag);
			result.setSuccess(true);			

        	Map<String, String> param = new HashMap<String, String>();
        	// 验签成功入账
			if (BocConstants.B2C_ORDER_STATUS_PAY.equals(messageB2C.getOrderStatus())) {					
				param.put("orderId", messageB2C.getOrderId());
				param.put("amt", messageB2C.getPayAmount());
				param.put("cardType", "Unknown");
				param.put("notifyNum", "1");
				param.put("payStatus", "00");
				param.put("retCode", "00");
				param.put("retMsg", "验签成功");
				param.put("signData", messageB2C.getSignMessage());
				param.put("isFront", String.valueOf(base.isFront()));
				result.setModel("isNotifyCashier", param);
				result.setOrderStatus(true);
			} else {
				// 通知后台地址该订单失败
				param.put("orderId", messageB2C.getOrderId());
				param.put("amt", messageB2C.getPayAmount());
				param.put("cardType", "Unknown");
				param.put("notifyNum", "1");
				param.put("payStatus", "01");
				param.put("retCode", "01");
				param.put("retMsg", "验签失败");
				param.put("signData", messageB2C.getSignMessage());
				param.put("isFront", String.valueOf(base.isFront()));
				result.setModel("isNotifyCashier", param);
				result.setOrderStatus(false);
			}				
        
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		}
		return result;
	}

	@Override
	public Result query(BaseMessage base) {
		Result result = new Result(false);
		try {
			BocB2cMessage messageB2C = new BocB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			BocB2cMessage b2c = (BocB2cMessage) bocB2cManager.query(messageB2C);
			result.setDefaultModel(b2c);
			result.setSuccess(true);
			if(BisPayCheckUtils.checkExternalOrderID(messageB2C.getOrderId())){
				Map<String, String> param = new HashMap<String, String>();
				param.put("orderId", messageB2C.getOrderId());
				param.put("amount", messageB2C.getPayAmount());
				if (b2c == null) {
					// 查询订单为空
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", "银行返回对账结果为空");
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}else if (StringUtils.isNotBlank(b2c.getException())) {
					// 查询订单错误
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", b2c.getException());
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}else{
					List<BocB2cMessage> bocB2cMessageList = b2c.getList();
					if (bocB2cMessageList != null && bocB2cMessageList.size() > 0) {
						param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
						BocB2cMessage orderRes = (BocB2cMessage) b2c.getList().get(0);
						if (StringUtils.equals(BocConstants.B2C_ORDER_STATUS_FAIL,orderRes.getOrderStatus())) {
							param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
							param.put("returnMessage", "交易失败");
						}
						//成功处理
						else if (StringUtils.equals(BocConstants.B2C_ORDER_STATUS_PAY,orderRes.getOrderStatus())) {
							param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
							param.put("returnMessage", "交易成功");
						}
						//不处理
						else if (StringUtils.equals(BocConstants.B2C_ORDER_STATUS_UNDEAL,orderRes.getOrderStatus())) {
							param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
							param.put("returnMessage", "交易未做处理");
						} else if (StringUtils.equals(BocConstants.B2C_ORDER_STATUS_CANCEL,orderRes.getOrderStatus())) {
							param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
							param.put("returnMessage", "交易被撤销");
						} else if (StringUtils.equals(BocConstants.B2C_ORDER_STATUS_RETURN_GOODS,orderRes.getOrderStatus())) {
							param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
							param.put("returnMessage", "交易退货");
						} else if (StringUtils.equals(BocConstants.B2C_ORDER_STATUS_UNKNOW,orderRes.getOrderStatus())) {
							param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
							param.put("returnMessage", "交易发生未明情况");
						} else {
							param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
							param.put("returnMessage", "交易发生异常");
						}
					}
				}
				result.setModel("queryResult", param);
			}
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
		}
		return result;
	}
	
	/**
	 * base fileDate
	 */
	@Override
	public Result dayend(BaseMessage base) {
		Result result = new Result(false);
		try {
			BocDayEndMessage dayendMsg = new BocDayEndMessage();
			BeanUtils.copyProperties(base, dayendMsg);
			dayendMsg =  (BocDayEndMessage) bocB2cManager.dayend(dayendMsg);
			result.setDefaultModel(dayendMsg);
			result.setSuccess(true);
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
			result.setSuccess(false);
		} catch (Exception e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(ProcCodeConstants.PROC_CODE_300G1000);
			result.setSuccess(false);
		}
		return result;
	}

	@Override
	public Result refund(BaseMessage arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}
}
